package com.neuedu.web.controller;

import com.neuedu.web.utils.JSONUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 金山
 * 项目：javaweb
 * site: https://blog.fulfill.com.cn
 * 描述
 * @data 2024/11/1814:04
 */
@WebServlet("/visit")
public class VisitedController  extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        // req.getSession()
        //只要有请求，就计数 +1
        ServletContext application = req.getServletContext();
        System.out.println("application = " + application);

        if(application.getAttribute("visitNum") != null){
            Integer nums = (Integer) application.getAttribute("visitNum");
            nums++;
            application.setAttribute("visitNum",nums);

        }else{
            application.setAttribute("visitNum",1);
        }

        // application.removeAttribute("");


        Map map = new HashMap();
        map.put("success",true);
        map.put("visitNum",application.getAttribute("visitNum"));

        JSONUtils.write(resp,map);


    }
}
